package com.corecloud.mapper.mysql;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.corecloud.entity.SysPermissionRoleEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author CCW
 * @email 452050507@qq.com
 * @date 2020-08-25 18:11:36
 */
@Mapper
public interface SysPermissionRoleMapper extends BaseMapper<SysPermissionRoleEntity> {



	/**
	 * 根据条件查询SysPermissionRoleEntity信息
	 *
	 * @param map
	 * @return SysPermissionRoleEntity信息集合
	 */
	@Select({"<script>select * from sys_permission_role ",
			"where 1=1 ",
			"<if test='id !=null'> and id = #{id} </if>",
			"<if test='rid !=null'> and rid = #{rid} </if>",
			"<if test='pid !=null'> and pid = #{pid} </if>",
			"<if test='permissionUrl != null and permissionUrl != \"\" '> and permission_url like CONCAT('%',#{permissionUrl},'%') </if>",
			"<if test='permissionName != null and permissionName != \"\" '> and permission_name like CONCAT('%',#{permissionName},'%') </if>",
			"<if test='permissionState !=null'> and permission_state = #{permissionState} </if>",
			"<if test='roleCode != null and roleCode != \"\" '> and role_code like CONCAT('%',#{roleCode},'%') </if>",
			"<if test='roleName != null and roleName != \"\" '> and role_name like CONCAT('%',#{roleName},'%') </if>",
			"</script>"})
	List<SysPermissionRoleEntity> getListByParas(Map<String, Object> map);


	/**
	 * 根据权限ID更新权限与角色关联表信息
	 * @param pid
	 * @return
	 */
	@Update({"UPDATE ",
			"sys_permission_role spr,sys_permission sp ",
			"SET spr.permission_name = sp.NAME,spr.permission_url = sp.url,spr.permission_state = sp.is_valid,spr.permission_type = sp.type ",
			"WHERE spr.pid = sp.id and sp.id = #{pid} "})
	Integer updateInfoByPid(Integer pid);

	/**
	 * 根据角色ID更新权限与角色关联表信息
	 * @param rid
	 * @return
	 */
	@Update({"UPDATE sys_permission_role spr,sys_role sr SET spr.role_code = sr.`code`,spr.role_name = sr.`name` WHERE spr.rid = sr.id and sr.id = #{rid}"})
	Integer updateInfoByRid(Integer rid);



}
